package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.user.domain.LoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 部门接口
 *    crud
 *    分页+高级
 */
@RestController //@Controller+@ResponseBody
@RequestMapping("/department")
//@GetMapping =  @RequestMapping(value = "xxxx",method = RequestMethod.GET)
//@PostMapping = @RequestMapping(value = "xxxx",method = RequestMethod.POST)
//@DeleteMapping
//@PutMapping
//@PatchMapping
@Api(value = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //添加或修改-差异在于是否有id put
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        //通过id判断是添加或者修改
        try {
            if (department.getId()!=null){
                departmentService.update(department);
            }else {
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }

    }

    //删除   delete
    //写一个接口必须思考三个方面
    // 1 访问地址
    // 2 传递什么参数
    // 3 返回什么值 yes no JsonResult(yes/no msg)
    // 其实对于增删改，我们只关心是否成功，已经失败了的信息
    // 方案1： 字符串{success:true,message:"操作失败“}
    // 方案2： 写一个对象返回+@ResponseBody----》{success:true,message:"操作失败“}
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

        //1 垃圾版本
//        AjaxResult ajaxResult = new AjaxResult();
//        try {
//            departmentService.del(id);
//            return ajaxResult;
//        } catch (Exception e) {
//            e.printStackTrace();
//            ajaxResult.setSuccess(false);
//            ajaxResult.setMessage("删除失败,"+e.getMessage());
//            return ajaxResult;
//        }

        //2 稍微好一点
//        try {
//            departmentService.del(id);
//            return new AjaxResult();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new AjaxResult("删除失败!"+e.getMessage());
//        }

        //3 更好的方法
//        try {
//            departmentService.del(id);
//            return AjaxResult.me();
//        } catch (Exception e) {
//            e.printStackTrace();
//            AjaxResult me = AjaxResult.me();
//            me.setSuccess(false);
//            me.setMessage("删除失败!"+e.getMessage());
//            return me;
//        }

        //4 最好的方法
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //Get http://localhost:8080/deparment/1 deparment/2
    public Department getById(@PathVariable("id") Long id){
        return departmentService.getById(id);
    }

    //获取所有 get
    @GetMapping ///department
    public List<Department> getAll(){
        return departmentService.getAll();
    }

    //分页+高级查询 //post /department 传递对象都往@RequestBody
    @PostMapping
    public PageList<Department> list(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(request.getHeader("token")+"jfjjfjfjffh");
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        System.out.println(loginInfo);
        return departmentService.queryPage(query);
    }

    @PatchMapping //如果前台传递是json数组，后台可以使用List或者数组都可以
    public AjaxResult bulkDel(@RequestBody List<Long> ids){
        try {
            departmentService.bulkDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("批量删除失败！"+e.getMessage());
        }

    }

    //获取所有 get
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.queryDeptTree();
    }
}
